#include <QDateTime>
#include <QtSql>
#include <QDebug>
#include "dialog.h"
#include "ui_dialog.h"
#include "macro.h"
#include "eventop.h"
#include "Processes/threadprocess.h"
#include "Adapters/adapters.h"
#include "Adapters/AdapterTelnet/AdapterTelnet.h"
#include "Adapters/AdapterSerialPort/AdapterSerialPort.h"

void WeatherStation::slotProcessError(QByteArray processName, QByteArray strError)
{
    QList <QListWidgetItem*> listWI=ui->listWidget->findItems(processName,Qt::MatchExactly);
    slotShowErrorInfo("Error occured to "+processName+" :"+strError);
    if(listWI.size()>0)
    {
        listWI.at(0)->setTextColor(QColor::fromRgb(200));
    }
}

void WeatherStation::slotProcessFinished(QByteArray processName, int exitCode, QByteArray strStatus)
{
    QList <QListWidgetItem*> listWI=ui->listWidget->findItems(processName,Qt::MatchExactly);
    slotShowErrorInfo(processName+" finished"+" :"+QByteArray::number(exitCode)+strStatus);
    if(listWI.size()>0)
    {
        listWI.at(0)->setForeground(QColor::fromRgb(250,0,0,100));
    }
}
void WeatherStation::slotProcessStarted(QByteArray processName)
{
    QList <QListWidgetItem*> listWI=ui->listWidget->findItems(processName,Qt::MatchExactly);
    slotShowErrorInfo(processName+" started");
    if(listWI.size()>0)
    {
        listWI.at(0)->setTextColor(QColor::fromRgb(0,0,0));
    }
    else
        ui->listWidget->addItem(processName);
}
void WeatherStation::slotProcessStateChanged(QByteArray processName, QByteArray strState)
{
    slotShowErrorInfo(processName+" status changed :"+strState);

}

void WeatherStation::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    QByteArray itemText=item->text().toUtf8();
    emit sigProcessShow(itemText);
}

void WeatherStation::on_pBAdapterTelnet_clicked()
{
    adapters->adapterTelnet->show();
}

void WeatherStation::on_pBAdapterSerialPort_clicked()
{
    adapters->adapterSerialPort->show();
}

void WeatherStation::connectApdatersSigSlots()
{
    connect(adapters->adapterTelnet,SIGNAL(sigPacketNodeToServer(QByteArray,QHostAddress,quint16,bool)),
            this,SLOT(analyzeGWDatagram(QByteArray,QHostAddress,quint16,bool)));
    connect(adapters->adapterSerialPort,SIGNAL(sigPacketNodeToServer(QByteArray,QHostAddress,quint16,bool)),
            this,SLOT(analyzeGWDatagram(QByteArray,QHostAddress,quint16,bool)));
    connect(adapters->adapterSerialPort,SIGNAL(sigInfo(QString)),this,SLOT(slotApdTxt(QString)));
}
